<?php

namespace app\admin\controller\message;

use app\common\controller\Backend;

use think\Controller;
use think\Request;
use think\Db;

/**
 * @icon fa fa-circle-o
 */
class Message extends Backend
{
    
    /**
     * Message模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Message');
    }
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where = array();
            $where['c.is_delete'] = array('eq', 0);
            $total = $this->model
                ->alias('c')
                ->join('users u', 'c.user_id = u.user_id', 'LEFT')
                ->where($where)
                ->order($sort, $order)
                ->count();
            
            $list = $this->model
                ->alias('c')
                ->join('users u', 'c.user_id = u.user_id', 'LEFT')
                ->field('c.*, u.nickname')
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as &$v) {
                $v['have_see'] = $v['have_see']==1?'已查看':'未查看';
                $v['admin_id'] = '平台';
                $v['nickname'] = !empty($v['nickname'])?$v['nickname']:'全员消息';
            }
            unset($v);

            $result = array("total" => $total, "rows" => $list);
            
            return json($result);
        }
        $btns = array('refresh', 'add');
        $this->view->assign('btns', $btns);
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $inputData = input();
            $user_name = $inputData['user_name'];
            //user_name = 0表示发送全员消息
            if($user_name != 0) {
                $user_id = Db::name('users')->where(array('user_name' => $user_name))->value('user_id');
                if(empty($user_id)) {
                    $this->error('该用户不存在');
                    exit;
                }
            } else {
                $user_id = 0;
            }

            $add_data = array(
                'user_id' => $user_id,
                'content' => $inputData['content'],
                'admin_id' => 1,
                'have_see' => 0,
                'is_delete' => 0,
                'create_time' => date('Y-m-d H:i:s', time())
            );
            $re = $this->model->insert($add_data);
            if($re) {
                $this->success('创建成功');
            } else {
                $this->error('创建失败');
            }
        }
        
        return $this->view->fetch();
    }
    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $id = $ids;
        $row = $this->model->get($id);
        if($row['user_id'] != 0) {
            $row['user_name'] = Db::name('users')->where(array('user_id' => $row['user_id']))->value('user_name');
        } else {
            $row['user_name'] = 0;
        }

        if (!$row){
            $this->error('没有查到任何信息');
        }
        
        if ($this->request->isPost()) {
            $inputData = input();
            $save_data = array(
                'content' => $inputData['content']
            );
            $re = $this->model->where(array('id' => $id))->update($save_data);
            if($re !== false) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }
        }
        
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
    /**
     * 删除
     */
    public function del($ids = ""){
        $id = $ids;
        $re = Db::name('message')->where(array('id' => $id))->setField('is_delete', 1);
        if($re !== false) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
        
    }
}
